﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace CharityGamesOnline.Modules.Account
{
    public partial class PaymentSetting : ModuleSettingsBase
    {
        private string CultureCode
        {
            get { return LocaleController.Instance.GetCurrentLocale(PortalId).Code; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!Null.IsNull(ModuleSettings["ViewMode"]) && !string.IsNullOrEmpty((string)ModuleSettings["ViewMode"]))
                {
                    ddlViewMode.SelectedIndex = ddlViewMode.Items.IndexOf(ddlViewMode.Items.FindByValue((string)ModuleSettings["ViewMode"]));
                }

                List<TabInfo> listTabs = TabController.GetPortalTabs(TabController.GetTabsBySortOrder(PortalId, CultureCode, true), Null.NullInteger, true, "<" + Localization.GetString("None_Specified") + ">", true, false, false, false, false);
                ddlEventDetailTabID.DataSource = listTabs;
                ddlEventDetailTabID.DataBind();

                ddlGameDetailTabID.DataSource = listTabs;
                ddlGameDetailTabID.DataBind();

                ddlPayListTabID.DataSource = listTabs;
                ddlPayListTabID.DataBind();

                ddlPayTabID.DataSource = listTabs;
                ddlPayTabID.DataBind();

                if (!Null.IsNull(ModuleSettings["EventDetailTabID"]) && !string.IsNullOrEmpty((string)ModuleSettings["EventDetailTabID"]))
                {
                    ListItem selItem = ddlEventDetailTabID.Items.FindByValue(ModuleSettings["EventDetailTabID"].ToString());
                    if (selItem != null)
                        selItem.Selected = true;
                }

                if (!Null.IsNull(ModuleSettings["GameDetailTabID"]) && !string.IsNullOrEmpty((string)ModuleSettings["GameDetailTabID"]))
                {
                    ListItem selItem = ddlGameDetailTabID.Items.FindByValue(ModuleSettings["GameDetailTabID"].ToString());
                    if (selItem != null)
                        selItem.Selected = true;
                }

                if (!Null.IsNull(ModuleSettings["PayListTabID"]) && !string.IsNullOrEmpty((string)ModuleSettings["PayListTabID"]))
                {
                    ListItem selItem = ddlPayListTabID.Items.FindByValue(ModuleSettings["PayListTabID"].ToString());
                    if (selItem != null)
                        selItem.Selected = true;
                }

                if (!Null.IsNull(ModuleSettings["PayTabID"]) && !string.IsNullOrEmpty((string)ModuleSettings["PayTabID"]))
                {
                    ListItem selItem = ddlPayTabID.Items.FindByValue(ModuleSettings["PayTabID"].ToString());
                    if (selItem != null)
                        selItem.Selected = true;
                }

                if (!Null.IsNull(ModuleSettings["ShowSectionHead"]))
                {
                    cbShowSectionHead.Checked = Boolean.Parse(ModuleSettings["ShowSectionHead"].ToString());
                }

                if (!Null.IsNull(ModuleSettings["ShowModuleName"]))
                {
                    cbShowModuleName.Checked = Boolean.Parse(ModuleSettings["ShowModuleName"].ToString());
                }

                if (!Null.IsNull(ModuleSettings["RemoteHost"]) && !string.IsNullOrEmpty((string)ModuleSettings["RemoteHost"]))
                {
                    txtRemoteHost.Text = ModuleSettings["RemoteHost"].ToString();
                }

                if (!Null.IsNull(ModuleSettings["PayAmountSelectionContent"]) && !string.IsNullOrEmpty((string)ModuleSettings["PayAmountSelectionContent"]))
                {
                    txtPayAmountSelection.Text = ModuleSettings["PayAmountSelectionContent"].ToString();
                }

                if (!Null.IsNull(ModuleSettings["PayConfirmContent"]) && !string.IsNullOrEmpty((string)ModuleSettings["PayConfirmContent"]))
                {
                    txtPayConfirm.Text = ModuleSettings["PayConfirmContent"].ToString();
                }

                if (!Null.IsNull(ModuleSettings["GoToPayContent"]) && !string.IsNullOrEmpty((string)ModuleSettings["GoToPayContent"]))
                {
                    txtGoToPay.Text = ModuleSettings["GoToPayContent"].ToString();
                }

                if (!Null.IsNull(ModuleSettings["PopupContent"]) && !string.IsNullOrEmpty((string)ModuleSettings["PopupContent"]))
                {
                    txtPopupContent.Text = ModuleSettings["PopupContent"].ToString();
                }

            }
        }

        public override void UpdateSettings()
        {
            try
            {
                ModuleController objModules = new ModuleController();

                objModules.UpdateModuleSetting(ModuleId, "ViewMode", ddlViewMode.SelectedValue);
                objModules.UpdateModuleSetting(ModuleId, "EventDetailTabID", ddlEventDetailTabID.SelectedValue);
                objModules.UpdateModuleSetting(ModuleId, "GameDetailTabID", ddlGameDetailTabID.SelectedValue);                
                objModules.UpdateModuleSetting(ModuleId, "PayListTabID", ddlPayListTabID.SelectedValue);
                objModules.UpdateModuleSetting(ModuleId, "PayTabID", ddlPayTabID.SelectedValue);
                objModules.UpdateModuleSetting(ModuleId, "ShowSectionHead", cbShowSectionHead.Checked.ToString());
                objModules.UpdateModuleSetting(ModuleId, "ShowModuleName", cbShowModuleName.Checked.ToString());
                objModules.UpdateModuleSetting(ModuleId, "RemoteHost", txtRemoteHost.Text);
                objModules.UpdateModuleSetting(ModuleId, "PayAmountSelectionContent", txtPayAmountSelection.Text);
                objModules.UpdateModuleSetting(ModuleId, "PayConfirmContent", txtPayConfirm.Text);
                objModules.UpdateModuleSetting(ModuleId, "GoToPayContent", txtGoToPay.Text);
                objModules.UpdateModuleSetting(ModuleId, "PopupContent", txtPopupContent.Text);
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
    }
}